package com.tanhua.template;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.tanhua.properties.BaiduCensorProperties;
import org.json.JSONObject;

import java.util.List;

/**
 * @author liuyp
 * @date 2021/06/26
 */
public class BaiduCensorTemplate {
    private AipContentCensor client;

    public BaiduCensorTemplate(BaiduCensorProperties properties) {
        // 初始化一个AipContentCensor
        client = new AipContentCensor(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
    }

    /**
     * 审核图片内容
     * @param url 图片的访问地址
     * @return 审核结果。true审核通过；false审核不通过
     */
    public boolean imageCensor(String url){
        JSONObject response = client.imageCensorUserDefined(url, EImgType.URL, null);
        return response.getInt("conclusionType")==1;
    }

    /**
     * 审核文本内容
     * @param text 文本内容
     * @return 审核结果。true审核通过；false审核不通过
     */
    public boolean textCensor(String text) {
        // 调用接口进行审核
        JSONObject response = client.textCensorUserDefined(text);

        // 返回结果 1合规，2不合规，3疑似，4审核失败
        return response.getInt("conclusionType") == 1;
    }

    /**
     * 批量审核图片
     * @param urlList 图片的url地址集合
     * @return true全部审核通过；false审核不通过
     */
    public boolean imageBatchCensor(List<String> urlList){
        for (String url : urlList) {
            JSONObject response = client.imageCensorUserDefined(url, EImgType.URL, null);
            //1合规，2不合规，3疑似，4审核失败
            if (response.getInt("conclusionType") != 1) {
                return false;
            }
        }

        return true;
    }
}
